package com.dianrui.agent.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.dianrui.agent.R;
import com.dianrui.agent.adapter.PowerExchangeRecordAdapter;
import com.dianrui.agent.bean.ChargeRecord;
import com.dianrui.agent.bean.ChargeRecordList;
import com.dianrui.agent.bean.PlanningRecordList;
import com.dianrui.agent.okhttp.OkHttpRequest;
import com.dianrui.agent.util.SharedUtil;
import com.dianrui.agent.util.StringUtils;
import com.dianrui.agent.util.ToastUtil;
import com.dianrui.agent.util.Url;
import com.dianrui.agent.view.TrackDatePicker;
import com.google.gson.JsonObject;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class ExchangePowerRecordActivity extends BaseActivity {

    private String adminId;
    private int mPageIndex = 1;
    private String chooseDate;
    private String type="1";

    private List<ChargeRecordList> chargeRecordListAll = new ArrayList<>();
    private PowerExchangeRecordAdapter powerExchangeRecordAdapter;

    private List<ChargeRecordList> ChargeRecordListeffecTive = new ArrayList<>();
    private List<ChargeRecordList> ChargeRecordListInvalid = new ArrayList<>();

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.attendance_count)
    TextView attendanceCount;
    @BindView(R.id.month_count)
    TextView monthCount;
    @BindView(R.id.current_date)
    TextView currentDate;
    @BindView(R.id.listview)
    ListView listView;
    @BindView(R.id.emptyView)
    LinearLayout emptyView;
    @BindView(R.id.effective_btn)
    Button effectiveBtn;
    @BindView(R.id.invaild_btn)
    Button invaildBtn;



    @Override
    public int getContentViewId() {
        return R.layout.activity_powerrecord;
    }

    @Override
    public void initView() {
        if (getIntent() != null && getIntent().getExtras() != null) {
            Bundle bundle = getIntent().getExtras();
            if (bundle != null) {
                adminId = bundle.getString("adminId");
            }
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        Date date = new Date(System.currentTimeMillis());
        currentDate.setText(simpleDateFormat.format(date));
        title.setText("换电记录");
        powerExchangeRecordAdapter = new PowerExchangeRecordAdapter(ExchangePowerRecordActivity.this, ChargeRecordListeffecTive);
        listView.setAdapter(powerExchangeRecordAdapter);
        getCharingRecord(chooseDate);


    }

    private void getCharingRecord(String chooseDate) {
        if (ChargeRecordListeffecTive != null) {
            ChargeRecordListeffecTive.clear();
        }
        if (ChargeRecordListInvalid != null) {
            ChargeRecordListInvalid.clear();
        }

        if (chargeRecordListAll != null) {
            chargeRecordListAll.clear();
        }
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("manage_id", adminId);
        if (!StringUtils.isEmpty(chooseDate)) {
            jsonObject.addProperty("month_time", chooseDate);
        }else {
            jsonObject.addProperty("month_time", currentDate.getText().toString());
        }
        jsonObject.addProperty("p", mPageIndex);
        OkHttpRequest.getInstance().postTokenRequests(SharedUtil.getUrl() + Url.CHRAINGRECORED, jsonObject.toString(), new OkHttpRequest.XCallBack() {

            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        if (null != jsonObject && !isFinishing()) {
                            if ("1000".equals(jsonObject.optString("status"))) {

                                attendanceCount.setText(jsonObject.optJSONObject("data").optString("attendance_count"));
                                JSONArray jsonArray = jsonObject.optJSONObject("data").optJSONArray("list");
                                if (jsonArray.length() > 0) {
                                    for (int i = 0; i < jsonArray.length(); i++) {
                                        ChargeRecordList chargeRecordList = new ChargeRecordList();
                                        JSONObject jsonObject1 = jsonArray.optJSONObject(i);
                                        chargeRecordList.date_title = jsonObject1.optString("date_title");
                                        JSONArray jsonArray1 = jsonObject1.optJSONArray("data");
                                        if (jsonArray1.length() > 0) {
                                            List<ChargeRecord> chargeRecordLists = new ArrayList<>();
                                            for (int j = 0; j < jsonArray1.length(); j++) {
                                                ChargeRecord chargeRecord = new ChargeRecord();
                                                chargeRecord.address = jsonArray1.optJSONObject(j).optString("address");
                                                chargeRecord.create_time = jsonArray1.optJSONObject(j).optString("create_time");
                                                chargeRecord.number = jsonArray1.optJSONObject(j).optString("number");
                                                chargeRecord.state = jsonArray1.optJSONObject(j).optInt("state");
                                                chargeRecord.before_voltage = jsonArray1.optJSONObject(j).optString("before_voltage");
                                                chargeRecord.later_voltage = jsonArray1.optJSONObject(j).optString("later_voltage");
                                                chargeRecordLists.add(chargeRecord);
                                            }
                                            chargeRecordList.data = chargeRecordLists;
                                        }
                                        chargeRecordListAll.add(chargeRecordList);
                                    }
                                    for (int i = 0; i < chargeRecordListAll.size(); i++) {
                                        for (int j = 0; j < chargeRecordListAll.get(i).data.size(); j++) {
                                            if (chargeRecordListAll.get(i).data.get(j).state==1) {
                                                ChargeRecordList listbean = new ChargeRecordList();
                                                listbean.date_title = chargeRecordListAll.get(i).date_title;
                                                listbean.data.add(chargeRecordListAll.get(i).data.get(j));
                                                ChargeRecordListeffecTive.add(listbean);//有效
                                            } else if (chargeRecordListAll.get(i).data.get(j).state==2) {
                                                ChargeRecordList listbean = new ChargeRecordList();
                                                listbean.date_title = chargeRecordListAll.get(i).date_title;
                                                listbean.data.add(chargeRecordListAll.get(i).data.get(j));
                                                ChargeRecordListInvalid.add(listbean);//无效

                                            }
                                        }

                                    }
                                    if (type.equals("1")) {
                                        if (powerExchangeRecordAdapter != null) {
                                            powerExchangeRecordAdapter.refresh(ChargeRecordListeffecTive);
                                        }
                                        monthCount.setText(ChargeRecordListeffecTive.size()+"");
                                    }else {
                                        if (powerExchangeRecordAdapter != null) {
                                            powerExchangeRecordAdapter.refresh(ChargeRecordListInvalid);
                                        }
                                        monthCount.setText(ChargeRecordListInvalid.size()+"");
                                    }
                                }

                            }else if ("1001".equals(jsonObject.optString("status"))) {
                                ToastUtil.showToast(jsonObject.optString("message"));
                            } else if ("1002".equals(jsonObject.optString("status"))) {
                                spUtils.clear();
                                ToastUtil.showToast(jsonObject.optString("message"));
                                JumpActivitys(LoginActivity.class);
                                finish();
                            }
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            }

            @Override
            public void FailedRequest(String result) {
                ToastUtil.showToast(getString(R.string.net_error));
            }
        });

    }


    @OnClick({R.id.back, R.id.calendar_img,R.id.effective_btn, R.id.invaild_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;

            case R.id.calendar_img:
                TrackDatePicker datePicker = new TrackDatePicker(ExchangePowerRecordActivity.this);
                datePicker.setDateListener(new TrackDatePicker.OnDateCListener() {
                    @Override
                    public void onDateSelected(String year, String month) {
                        chooseDate = year + "-" + month;
                        currentDate.setText(year + "-" + month);
                        getCharingRecord(chooseDate);
                    }
                });
                datePicker.show();
                break;

            case R.id.effective_btn:
                type="1";
                effectiveBtn.setBackgroundResource(R.drawable.effect_shape);
                invaildBtn.setBackgroundResource(R.drawable.invaild_shape);
                effectiveBtn.setTextColor(getResources().getColor(R.color.white));
                invaildBtn.setTextColor(getResources().getColor(R.color.black));
                effectiveList();
                break;

            case R.id.invaild_btn:
                type="2";
                effectiveBtn.setBackgroundResource(R.drawable.effect_shapes);
                invaildBtn.setBackgroundResource(R.drawable.invaild_shapes);
                effectiveBtn.setTextColor(getResources().getColor(R.color.black));
                invaildBtn.setTextColor(getResources().getColor(R.color.white));
                invaildList();
                break;


            default:
                break;
        }
    }

    private void effectiveList() {
        powerExchangeRecordAdapter.refresh(ChargeRecordListeffecTive);
        monthCount.setText(ChargeRecordListeffecTive.size() + "");
    }

    private void invaildList() {
        powerExchangeRecordAdapter.refresh(ChargeRecordListInvalid);
        monthCount.setText(ChargeRecordListInvalid.size() + "");
    }


}
